package org.example.Imp;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import exam.dao.TeacherDao;
import org.example.TeacherService;
import org.example.domain.Teacher;
import org.example.pageParam.CommonData;
import org.example.vo.FileUploadVO;
import org.example.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImp implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public Teacher selectOneTeacherByMnemonic_code(String name) {
        return teacherDao.selectOneTeacherByMnemonic_code(name);
    }

    @Override
    public boolean judgePassword(String originPass, String newPass) {
        newPass = DigestUtil.md5Hex(newPass);
        if (originPass.equals(newPass)){
            return true;
        }
        return false;
    }

    @Override
    public Boolean surePassword(String loginPwd, String inputPwd) {
        String tempInput = DigestUtil.md5Hex(inputPwd);
        if (!loginPwd.equals(tempInput)){
            return false;
        }
        return true;
    }

    @Override
    public void commitUpdatePwd(Map<String,String> map) {
        String temp = DigestUtil.md5Hex(map.get("newPwd"));
        map.put("newPwd",temp);
        teacherDao.updatePwd(map);
    }

    @Override
    public PageVO showTeacherList(String filterName, Integer page, Integer count) {
        page = page<=0?1:page;
        //还要保证page的上限  不能超过数据库数据分页的页数
        //如何找到数据库所能分页的最大数据呢？  可以通过查询数据库中的所有数据个数/每页展示的个数count
        // 有一个问题 这个数据库的总数 需要判断是否需要过滤 filterName是否等于空
        Integer realTotal = teacherDao.selectTeacherTotal(filterName);
        realTotal = realTotal==0?1:realTotal;
        //realPage是数据库中的数据按照count能分的页数
        Integer realPage = realTotal%count==0?realTotal/count:realTotal/count+1;
        page = page>realPage?realPage:page;
        //这里就可以从数据库中获取数据了  但是有一个问题  MySQL数据库用的是limit分页 需要start和length 需要转化
        Integer start = (page-1)*count;
        Integer length = count;
        Map<String,Object> map = new HashMap<>();
        map.put("start",start);
        map.put("length",length);
        map.put("filterName",filterName);
        List<Teacher> teacherList = teacherDao.selectTeacherList(map);
        PageVO pageVO = new PageVO();
        pageVO.setFilterName(filterName);
        pageVO.setPage(page);
        pageVO.setCount(count);
        pageVO.setParams(teacherList);
        pageVO.setTotal(realTotal);
        pageVO.setRealPage(realPage);
        pageVO.setLength(length);
        pageVO.setStart(start);
        pageVO.setPageStart(page);
        pageVO.setPageEnd(realPage);
//        System.out.println(pageVO);
        return pageVO;
    }

    @Override
    public void saveOneTeacher(String tname) {
        Teacher teacher = new Teacher();
        teacher.setTname(tname);
        teacher.setMnemonic_code(PinyinUtil.getPinyin(tname,""));
        teacher.setPass(DigestUtil.md5Hex(CommonData.DEFAULT_PASS));
        try {
            teacherDao.saveOne(teacher);
        }catch (Exception e){
//            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public Teacher selectOneTeacherById(Long id) {
        return teacherDao.selectOneTeacherById(id);
    }

    @Override
    public String editOneTeacher(Teacher teacher) {
        List<Teacher> teacherList = teacherDao.selectListTeacherByName(teacher.getTname());
        if (teacherList.size()>0){
            return "0";
        }
        teacher.setMnemonic_code(PinyinUtil.getPinyin(teacher.getTname(),""));
        teacherDao.updateOneTeacher(teacher);
        return "1";
    }

    @Override
    public void deleteOne(Long id) {
        teacherDao.deleteOneTeacher(id);
    }

    @Override
    public void deleteAll(String ids) {
        String[] idS = ids.split(",");
        for (int i=0;i< idS.length;i++){
            teacherDao.deleteOneTeacher(Long.parseLong(idS[i]));
        }
    }

    @Override
    public List<Teacher> selectAll() {
        return teacherDao.selectAll();
    }

    @Override
    public FileUploadVO saveListTeacher(List<Teacher> teacherList) {
        Integer total = teacherList.size();
        Integer successCount = 0;
        Integer failCount = 0;
        String result = "";
        for (int i = 0 ;i<teacherList.size();i++){
            try {
                saveOneTeacher(teacherList.get(i).getTname());
                successCount += 1;
                result +="第"+(i+1)+"号用户，存储成功|";
            }catch (Exception e){
                failCount += 1;
                result+="第"+(i+1)+"号用户，存储失败，用户名重复|";
            }
        }
        return new FileUploadVO(successCount,failCount,result);
    }
}
